
const HOST = 'http://www.creb.com.cn'

export const GET = (name, params, extendHeader) => {
  let reqURL = ''
  if (name && name.startsWith('/')) {
    reqURL = `${HOST}${name}`
  } else {
    reqURL = `${HOST}/${name}`
  }
  const header = {
    'content-type': 'application/json',
    ...extendHeader
  }

  return new Promise((resolve, reject) => {
    uni.request({
      url: reqURL,
      data: params,
      header,
      success(res) {
        if (res.statusCode === 200) {
          resolve(res.data.data)
        } else {
          console.log(`GET ${reqURL} ${params}`, res.data)
          const message =
            (res.data.error && res.data.error.error) ||
            res.data.message ||
            res.data.toString()
          reject(message || '未知错误')
          if (res.statusCode === 401) {
            // token过期，重新登录
            uni.navigateTo({ url: '/pages/index/mobile?delta=5' })
          }
        }
      },
      fail(err) {
        console.log('fail', err)
        let message = err && err.toString()
        if (err.errMsg.indexOf('request:fail') !== -1) {
          message = '网络不给力，请检查'
        } else if (message === '[object Object]') {
          message = JSON.stringify(err)
        }
        reject(message || '未知错误')
      },
    })
  })
}

export const POST = (name, params, extendHeader) => {
  let reqURL = ''
  if (name && name.startsWith('/')) {
    reqURL = `${HOST}${name}`
  } else {
    reqURL = `${HOST}/${name}`
  }
  const header = {
    'content-type': 'application/json',
    ...extendHeader
  }

  return new Promise((resolve, reject) => {
    uni.request({
      url: reqURL,
      data: params,
      header,
      method: 'POST',
      success(res) {
        if (res.statusCode === 200) {
          resolve(res.data.data)
        } else {
          console.log('POST', reqURL, params, res.data)
          const message =
            (res.data.error && res.data.error.error) ||
            res.data.message ||
            res.data.toString()
          reject(message || '未知错误')
          if (res.statusCode === 401) {
            // token过期，重新登录
            uni.navigateTo({ url: '/pages/index/mobile?delta=5' })
          }
        }
      },
      fail(err) {
        console.log('fail', err)
        let message = err && err.toString()
        if (err.errMsg.indexOf('request:fail') !== -1) {
          message = '网络不给力'
        } else if (message === '[object Object]') {
          message = JSON.stringify(err)
        }
        reject(message || '未知错误')
      },
    })
  })
}

export const DELETE = (name, params, extendHeader) => {
  let reqURL = ''
  if (name && name.startsWith('/')) {
    reqURL = `${HOST}${name}`
  } else {
    reqURL = `${HOST}/${name}`
  }
  const header = {
    'content-type': 'application/json',
    ...extendHeader
  }

  return new Promise((resolve, reject) => {
    uni.request({
      url: reqURL,
      data: params,
      header,
      method: 'DELETE',
      success(res) {
        if (res.statusCode === 200) {
          resolve(res.data.data)
        } else {
          console.log('DELETE', reqURL, res.data)
          const message =
            (res.data.error && res.data.error.error) || res.data.message
          reject(message || '未知错误')
          if (res.statusCode === 401) {
            // token过期，重新登录
            uni.navigateTo({ url: '/pages/index/mobile?delta=5' })
          }
        }
      },
      fail(err) {
        console.log('fail', err)
        let message = err && err.toString()
        if (err.errMsg.indexOf('request:fail') !== -1) {
          message = '网络不给力'
        } else if (message === '[object Object]') {
          message = JSON.stringify(err)
        }
        reject(message || '未知错误')
      },
    })
  })
}

export const PUT = (name, params, extendHeader) => {
  let reqURL = ''
  if (name && name.startsWith('/')) {
    reqURL = `${HOST}${name}`
  } else {
    reqURL = `${HOST}/${name}`
  }
  const header = {
    'content-type': 'application/json',
    ...extendHeader
  }

  return new Promise((resolve, reject) => {
    uni.request({
      url: reqURL,
      data: params,
      header,
      method: 'PUT',
      success(res) {
        if (res.statusCode === 200) {
          resolve(res.data.data)
        } else {
          console.log('PUT', reqURL, res.data)
          const message =
            (res.data.error && res.data.error.error) ||
            res.data.message ||
            res.data.toString()
          reject(message || '未知错误')
          if (res.statusCode === 401) {
            // token过期，重新登录
          }
        }
      },
      fail(err) {
        console.log('fail', err)
        let message = err && err.toString()
        if (err.errMsg.indexOf('request:fail') !== -1) {
          message = '网络不给力'
        } else if (message === '[object Object]') {
          message = JSON.stringify(err)
        }
        reject(message || '未知错误')
      },
    })
  })
}
